package mobidever.mygodutch.database.base;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

import mobidever.mygodutch.utility.Reflection;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLiteHelper extends SQLiteOpenHelper
{
	
	private static SQliteDataBaseConfig mSQliteDataBaseConfig;
	private static Context mContext;
	private static SQLiteHelper INSTANCE;

	public interface SQLiteDataTable
	{
		public void onCreate(SQLiteDatabase pDatabase);
		public void	onUpdate(SQLiteDatabase pDatabase);
	}
	
	private SQLiteHelper()
	{
		super(mContext,mSQliteDataBaseConfig.getDataBaseName(),null,mSQliteDataBaseConfig.getDataBaseVersion());
		
	}

	public static SQLiteHelper getSqLiteHelper(Context pContext)
	{
		mContext = pContext;
		if (mSQliteDataBaseConfig == null)
		{
			mSQliteDataBaseConfig = SQliteDataBaseConfig.getInstance(mContext);
		}
		if (INSTANCE == null)
		{
			INSTANCE = new SQLiteHelper();
		}
		return INSTANCE;
	}
	
	@Override
	public void onCreate(SQLiteDatabase pDatabase)
	{
		ArrayList<String> _ArrayList = mSQliteDataBaseConfig.getTable();
		Reflection _Reflection = new Reflection();
		for (int i = 0; i < _ArrayList.size(); i++)
		{
			try
			{
				((SQLiteDataTable)_Reflection.newInstance(_ArrayList.get(i), new Object[]{mContext}, new Class[] {Context.class})).onCreate(pDatabase);
			}  catch (Exception e)
			{
				e.printStackTrace();
			}
		}
  	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		// TODO Auto-generated method stub

	}

}
